import Vue from 'vue'
import {
	createRouter,
	createWebHashHistory
} from "vue-router";


/* Layout */
import Layout from '@/layout'
import store from '@/store'
export const constantRoutes = [{
		path: '/',
		redirect: '/home'
	},
	{
		path: '/redirect',
		component: Layout,
		hidden: true,
		children: [{
			path: '/redirect/:path(.*)',
			component: () => import('@/views/login/index')
		}]
	},
	{
		path: "/loginText",
		hidden: true,
		component: () => import("@/views/login/index.vue"),
		meta: {
			title: "登录",
		},
	},
	{
		path: "/login",
		hidden: true,
		component: () => import("@/views/login/index.vue"),
		meta: {
			title: "入口",
		},
	},
	{
		component: Layout,
		hidden: true,
		children: [{
				path: "/index",
				name: "Index",
				component: () => import("@/views/home/home.vue"),
				// component: () => import("@/views/index"),
				meta: {
					title: "首页",
					icon: "dashboard",
					affix: true,
				},
			},
			{
				path: "/order",
				name: "order",
				component: () => import("@/views/order/index.vue"),
				// component: () => import("@/views/index"),
				meta: {
					title: "订单",
					icon: "dashboard",
					affix: true,
				},
			},
			{
				path: "/orderFormModal",
				name: "orderFormModal",
				component: () => import("@/views/order/form.vue"),
				// component: () => import("@/views/index"),
				meta: {
					title: "订单表单",
					icon: "dashboard",
					affix: true,
				},
			},
			{
				path: "/product",
				name: "product",
				component: () => import("@/views/product/index.vue"),
				// component: () => import("@/views/index"),
				meta: {
					title: "商品管理",
					icon: "dashboard",
					affix: true,
				},
			},
			{
				path: "/productFormModal",
				name: "productFormModal",
				component: () => import("@/views/product/form.vue"),
				// component: () => import("@/views/index"),
				meta: {
					title: "商品管理表单",
					icon: "dashboard",
					affix: true,
				},
			},
			{
				path: "/productType",
				name: "productType",
				component: () => import("@/views/productType/index.vue"),
				// component: () => import("@/views/index"),
				meta: {
					title: "产品分类",
					icon: "dashboard",
					affix: true,
				},
			},
			{
				path: "/productTypeFormModal",
				name: "productTypeFormModal",
				component: () => import("@/views/productType/form.vue"),
				// component: () => import("@/views/index"),
				meta: {
					title: "产品分类表单",
					icon: "dashboard",
					affix: true,
				},
			},
			{
				path: "/user",
				name: "user",
				component: () => import("@/views/system/user/index.vue"),
				// component: () => import("@/views/index"),
				meta: {
					title: "用户",
					icon: "dashboard",
					affix: true,
				},
			},
		],
	},
	{
		component: Layout,
		hidden: true,
		children: [{
			path: "/grzx",
			component: () => import("@/views/system/user/userinfo"),
			name: "userinfo",
			meta: {
				title: "个人中心",
				icon: "dashboard",
				affix: false,
			},
		}],
	},
	{
		path: "/system/role-auth",
		component: Layout,
		hidden: true,
		children: [{
			path: "user/:roleId(\\d+)",
			component: () => import("@/views/system/role/authUser.vue"),
			name: "AuthUser",
			meta: {
				title: "分配用户",
				activeMenu: "/system/role",
			},
		}, ],
	},
	{
		path: '/system/user-auth',
		component: Layout,
		hidden: true,
		children: [{
			path: 'role/:userId(\\d+)',
			component: () => import('@/views/system/user/authRole.vue'),
			name: 'AuthRole',
			meta: {
				title: '分配角色',
				activeMenu: '/system/user'
			}
		}]
	},
	{
		path: '/system/job-log',
		component: Layout,
		hidden: true,
		children: [{
			path: 'index',
			component: () => import('@/views/system/job/log'),
			name: 'JobLog',
			meta: {
				title: '调度日志',
				activeMenu: '/system/job'
			}
		}]
	},
	{
		path: "/401",
		component: () => import("@/views/error/401"),
		hidden: true,
	},
	{
		path: "/:pathMatch(.*)*",
		component: () => import("@/views/error/404"),
		meta: {
			title: "找不到页面",
		},
	},
]
//创建路由对象
const router = createRouter({
	history: createWebHashHistory(),
	scrollBehavior: (to, from, savedPosition) => {
		store.commit('user/SET_ROUTER_POSITION', savedPosition || {})
		if (savedPosition) {
			return savedPosition
		} else {
			if (from.meta.keepAlive) {
				from.meta.savedPosition = document.body.scrollTop
			}
			return {
				x: 0,
				y: to.meta.savedPosition || 0
			}
		}
	},
	routes: constantRoutes
});
export function resetRouter() {
	const createRouter = () =>
		new Router({
			mode: 'history',
			routes: constantRoutes
		})
	// 用初始化的matcher替换当前router的matcher
	router.matcher = createRouter.matcher
}
export default router